const pageHelper = require('../../../../../helper/page_helper.js');
const ProjectBiz = require('../../../biz/project_biz.js');
const ProjectSetting = require('../../../public/project_setting.js');
const PassportBiz = require('../../../../../comm/biz/passport_biz.js');
const RequestBiz = require('../../../../../comm/biz/request_biz.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		cur: 'new',
		newList: null,
		myList: null,
		activityLoad: false,
    cateList: ProjectSetting.ACTIVITY_CATE,
    date: '',
    time: '',
    week: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		ProjectBiz.initPage(this);
	},

	_loadList: async function () {
		let user = PassportBiz.getToken();
		let req = RequestBiz.request('/api/ysjl/activity/home', {id: user ? user.id : ''});
		req.then(res => {
			if (res.data.ret == 0) {
				let data = res.data;
				this.setData({
					newList: data.data.newList,
					myList: data.data.myList,
					activityLoad: true
				})
			}
		})

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function () {
		let now = new Date(); // 获取当前时间
		let year = now.getFullYear(); // 获取当前年份
		let month = now.getMonth() + 1; // 获取当前月份，加“1”是因为月份是从0开始计算的，但实际月份是从1开始的
    let day = now.getDate(); // 获取当前日期
    let date = year + '/' + ('00' + month).substr(-2) + '/' + ('00' + day).substr(-2);
    let time = now.getHours()+ ":" + ('00' + now.getMinutes()).substr(-2);
    let week = ProjectSetting.WEEKDAYS[now.getDay()];
    this.setData({
      date: date,
      time: time,
      week: week
    });
    await this._loadUser();
		this._loadList();
	},

	onPullDownRefresh: async function () {
		await this._loadList();
		wx.stopPullDownRefresh();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	url: async function (e) {
		pageHelper.url(e, this);
	},


	bindCurTap: function (e) {
		let cur = pageHelper.dataset(e, 'cur');
		this.setData({ cur });
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

  },
  
	_loadUser: async function (e) {
    if (PassportBiz.isLogin()) {
      return;
    }
		let _self = this;
		wx.login({
			success (res) {
				if (res.code) {
					let req = RequestBiz.request('/api/ysjl/user/wxAuth', {code: res.code});
					req.then(res => {
						if (res.data.ret == 0) {
							let retData = res.data.data;
							if (retData.id) {
								PassportBiz.setToken(retData);
								wx.reLaunch({ url: '../index/my_index' });
							}
							else {
								_self.setData({
									isLoad: true,
									openid: retData.openid
								})
							}
						}
					});
				}
			}
		});
	},

})